package com.souku.finance.application.ui.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshScrollView;
import com.souku.finance.application.R;
import com.souku.finance.application.base.BaseFragment;
import com.souku.finance.application.bean.IndexBean;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.SuccessError;
import com.souku.finance.application.ui.VersionNewActivity;
import com.souku.finance.application.ui.invite.ShouKuTenderDetailsActivity;
import com.souku.finance.application.ui.invite.TenderDetailsActivity;
import com.souku.finance.application.ui.more.NoticeActivity;
import com.souku.finance.application.ui.more.RequestActivity;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.widget.BannerViewPager;
import com.umeng.analytics.MobclickAgent;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/5/5 0005.
 */

public class Home2Fragment extends BaseFragment {

    View rootView;
    @BindView(R.id.banner_view_pager)
    BannerViewPager bannerViewPager;
    @BindView(R.id.iv_news)
    ImageView ivNews;
    //    @BindView(R.id.circle)
//    ColorfulRingProgressView circle;
    @BindView(R.id.tv_circle_text)
    TextView tvCircleText;
    @BindView(R.id.tv_null)
    TextView tvNull;
    @BindView(R.id.iv_invite_politeness)
    ImageView ivInvitePoliteness;
    @BindView(R.id.pullView)
    PullToRefreshScrollView pullView;
    @BindView(R.id.tv_min)
    TextView tvMin;
    @BindView(R.id.rl_shoukubao)
    RelativeLayout rlShoukubao;
    @BindView(R.id.tv_more)
    TextView tvMore;
    @BindView(R.id.tv_day_rate)
    TextView tvDayRate;
    @BindView(R.id.textView7)
    TextView textView7;

    private IndexBean bean;
    Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fra_home2, container, false);
        unbinder = ButterKnife.bind(this, rootView);
        setContent(rootView);
        pullView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                getIndex();
            }
        });
        pullView.setRefreshing(true);
        return rootView;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        circle.setPercent(100);
    }

    private void getIndex() {
        HashMap<String, String> map = ParamUtil.getParams();
        RequestManager.getInstance().post(UrlConstact.INDEX, ParamUtil.getSing(map), new ReqListener(getActivity(), new SuccessError() {
            @Override
            public void onSuccess(String response) {
                pullView.onRefreshComplete();
                if (JsonUtil.getCode(response) == 1) {
                    bean = JsonUtil.fastBean(JsonUtil.getData(response).toString(), IndexBean.class);
                    setData();
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                pullView.onRefreshComplete();
                ToastUtil.show("网络错误");
            }
        }), ParamUtil.getHeaders());
    }

    @Override
    public void internet() {
        pullView.setRefreshing(true);
    }

    @Override
    public void showFragment() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    private void setData() {

        tvDayRate.setText("每" + bean.getRiseDuration() + "天利润上涨" + bean.getRiseRate() + "%");

        tvCircleText.setText(bean.getRate() + "%");

        tvMin.setText(MyTextUtil.setMoneyToElement(bean.getMinInvestment()) + "元");

        final List<IndexBean.BannersBean> beans = bean.getBanners();
        bannerViewPager.setBannerData(beans);
        bannerViewPager.setOnClickImage(new BannerViewPager.OnImageListener() {
            @Override
            public void onClick(View view, int p) {
                Intent intent = new Intent();
                intent.putExtra("webUrl", beans.get(p).getUrl());
                intent.setClass(getActivity(), BannerWebActivity.class);
                startActivity(intent);
            }
        });
    }

    @OnClick({R.id.iv_invite_politeness, R.id.rl_shoukubao, R.id.tv_more})
    public void onViewClicked(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.iv_invite_politeness:
                intent.setClass(getActivity(), RequestActivity.class);
                startActivity(intent);
                break;
            case R.id.rl_shoukubao:
                intent.setClass(getActivity(), ShouKuTenderDetailsActivity.class);
                if (bean != null) {
                    MobclickAgent.onEvent(getActivity(), "shoukuTenderDetails");
                    intent.putExtra(Constant.Default, bean.getId());
                    startActivity(intent);
                }
                break;
            case R.id.tv_more:
                intent.setClass(getActivity(), NoticeActivity.class);
                startActivity(intent);
                break;
        }
    }

    @OnClick(R.id.tv_day_rate)
    public void onViewClicked() {


    }
}
